<?php

// 账户安全

namespace app\shop\controller;

use think\Cache;
use think\Request;
use think\Session;
use think\Controller;
use app\shop\model\Member as MemberModel;

class AccountSecurity extends Controller
{
    /**
     * 账户安全
     * @return mixed
     */
    public function index()
    {
        // // 获取商品分类信息
        // $type_arr = $goodsType->where(['status' => 1])->field('id,parenTid,title')->select();
        // $menu = get_cate($type_arr);
        //会员ID
        $member_id = Session::get('member_id');

        // 用户信息
        $find = MemberModel::get($member_id);

        $pay_pass = $find['pay_pass'];

        return $this->fetch('', [
            'mate_head' => '账户安全',
            'pay_pass'  => $pay_pass,
        ]);
    }


    /**
     * 修改登录密码
     * @return array|mixed
     */
    public function loginPassword()
    {

        //会员ID
        $member_id = Session::get('member_id');

        if (Request::instance()->isPost()) {
            // 获取参数
            $data = Request::instance()->param();

            // 用户信息
            $find = MemberModel::get($member_id);

            // 判断旧密码
            if ($find['password'] != mcs($data['old_password'])) return ['code' => 404, 'message' => '再想想你的旧密码输入错误哦！！'];

            // 两次输入密码判断
            if ($data['new_password'] != $data['unpwd']) return ['code' => 404, 'message' => '两次密码输入不一致哦！！'];

            if (mcs($data['new_password']) == $find['password']) return ['code' => 404, 'message' => '新密码和旧密码不能一模一样哦！！！'];

            $operation = (new MemberModel())->allowField(true)->save(['password' => mcs($data['new_password'])], ['id' => $member_id]);

            if ($operation) {
                Session::delete('member_id');
                Session::delete('member_name');
                Session::delete('member_avatar');

                return ['code' => 200, 'message' => '密码就改成功！'];

            } else {
                return ['code' => 404, 'message' => '服务器异常,请稍后再试！'];
            }
        }

        return $this->fetch('account_security/login_password', [
            'mate_head' => '修改登录密码',
        ]);
    }


    /**
     * 修改支付密码
     * @return array|mixed
     */
    public function payPassword()
    {
        //会员ID
        $member_id = Session::get('member_id');

        if (Request::instance()->isPost()) {
            // 获取参数
            $data = Request::instance()->param();

            // 用户信息
            $find = MemberModel::get($member_id);

            // 就支付密码
            if ($find['pay_pass'] != mcs($data['old_password'])) return ['code' => 404, 'message' => '旧密码输入错误！'];

            // 两次输入密码判断
            if ($data['new_password'] != $data['unpwd']) return ['code' => 404, 'message' => '两次密码输入不一致！'];

            $operation = (new MemberModel())->allowField(true)->save(['pay_pass' => mcs($data['new_password'])], ['id' => $member_id]);

            if ($operation||$operation==0) {
                return ['code' => 200, 'message' => '密码修改成功！'];
            } else {
                return ['code' => 404, 'message' => '服务器异常,请稍后再试！'];
            }
        }


        return $this->fetch('account_security/pay_password', [
            'mate_head' => '修改支付密码',
        ]);
    }


    /**
     * 设置支付密码
     */
    public function setPassword()
    {
        //会员ID
        $member_id = Session::get('member_id');

        $find = MemberModel::get($member_id);

        if (Request::instance()->isPost()) {
            // 获取参数
            $data = Request::instance()->param();

            //获取验证码
            $code = Cache::get('pay_pass' . $data['username']);

            if ($code != $data['code']) return ['code' => 402, 'message' => '验证码错误！'];

            if ($data['pay_pass']!=$data['upwd']) return ['code' => 402, 'message' => '两次密码不一致！'];


            // 设置密码
            $operation = (new MemberModel())->allowField(true)->save(['pay_pass' => mcs($data['pay_pass'])], ['id' => $member_id]);

            if ($operation) {
                return ['code' => 200, 'message' => '密码设置成功！'];
            } else {
                return ['code' => 404, 'message' => '服务器异常,请稍后再试！'];
            }

        }


        return $this->fetch('account_security/set_password', [
            'mate_head' => '设置支付密码',
            'find'      => $find['username']
        ]);
    }
}